﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using MyRunDataBase;

namespace MyRun.Views
{
    public partial class RunHistory : PhoneApplicationPage
    {
        public RunHistory()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            ThreadPool.QueueUserWorkItem((para) => { LoadHistory(); });
        }

        private void LoadHistory()
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                using (var myfeetDB = new MyRunDataContent())
                {
                    lstSportHistory.ItemsSource = myfeetDB.Sports.OrderByDescending(p=>p.Time);
                }
            });
        }

        private void Item_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var sport = (sender as FrameworkElement).DataContext as SportModel;
            NavigationService.Navigate(new Uri("/Views/RunResult.xaml?id=" + sport.Id, UriKind.Relative));
        }
    }
}